import Taro from '@tarojs/taro'

// TabBar调试工具
export class TabBarDebugger {
  static log(message: string, data?: any) {
    console.log(`[TabBar Debug] ${message}`, data || '')
  }

  static error(message: string, error?: any) {
    console.error(`[TabBar Error] ${message}`, error || '')
  }

  static getCurrentPageInfo() {
    const pages = Taro.getCurrentPages()
    const currentPage = pages[pages.length - 1]
    
    this.log('当前页面信息:', {
      route: currentPage?.route,
      path: currentPage?.path,
      fullPath: currentPage?.fullPath
    })
    
    return currentPage
  }

  static getRouterInfo() {
    const router = Taro.getCurrentInstance().router
    
    this.log('路由信息:', {
      path: router?.path,
      params: router?.params,
      scene: router?.scene
    })
    
    return router
  }

  static testNavigation(path: string) {
    this.log(`测试导航到: ${path}`)
    
    return new Promise((resolve, reject) => {
      Taro.navigateTo({
        url: `/${path}`,
        success: (res) => {
          this.log('导航成功:', res)
          resolve(res)
        },
        fail: (error) => {
          this.error('导航失败:', error)
          reject(error)
        }
      })
    })
  }

  static testSwitchTab(path: string) {
    this.log(`测试switchTab到: ${path}`)
    
    return new Promise((resolve, reject) => {
      Taro.switchTab({
        url: `/${path}`,
        success: (res) => {
          this.log('switchTab成功:', res)
          resolve(res)
        },
        fail: (error) => {
          this.error('switchTab失败:', error)
          reject(error)
        }
      })
    })
  }

  static showDebugInfo() {
    const pageInfo = this.getCurrentPageInfo()
    const routerInfo = this.getRouterInfo()
    
    Taro.showModal({
      title: 'TabBar调试信息',
      content: `当前页面: ${pageInfo?.route || '未知'}
路由路径: ${routerInfo?.path || '未知'}
页面栈长度: ${Taro.getCurrentPages().length}`,
      showCancel: false
    })
  }
}

// 导出便捷函数
export const logTabBar = TabBarDebugger.log.bind(TabBarDebugger)
export const errorTabBar = TabBarDebugger.error.bind(TabBarDebugger)
export const getCurrentPageInfo = TabBarDebugger.getCurrentPageInfo.bind(TabBarDebugger)
export const testNavigation = TabBarDebugger.testNavigation.bind(TabBarDebugger)
export const testSwitchTab = TabBarDebugger.testSwitchTab.bind(TabBarDebugger)
